﻿/**********************************************************************************
 * 程序说明：     页面广告类别类(数据访问层)
 * 创建日期：     2009.6.13
 * 修改日期：     2012.02.15
 * 程序制作：     agui
 * 联系方式：     mailto:354990393@qq.com  
 * ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    /// <summary>
    /// 页面广告类别类
    /// </summary>
    public class UserGrade
    {


        public static readonly string currTableName = WebConfig.TablePrefix + "user_grade";
        private static Dictionary<string, string> grades;


        static UserGrade()
        {
            grades = new Dictionary<string, string>();
            IDataReader dr = GetReader(-1, "*", "");
            if (dr != null)
            {
                while (dr.Read())
                {
                    grades.Add(dr["grade"].ToString(), dr["grade_name"].ToString());
                }
                dr.Close();
            }
        }


        /// <summary>
        /// 得到模型
        /// </summary>
        /// <param grade_name="id"></param>
        /// <returns></returns>
        public UserGradeInfo GetUserGradeInfo(int id)
        {
            UserGradeInfo model = null;
            IDataReader dr = GetReader(1,"*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new UserGradeInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.grade = int.Parse(dr["grade"].ToString());
                    model.grade_name = dr["grade_name"].ToString();
                }
                dr.Close();
            }
            return model;

        }




        #region 添加，修改，删除操作

        /// <summary>
        /// 添加
        /// </summary>
        /// <param grade_name="model"></param>
        /// <returns></returns>
        public bool Add(UserGradeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " +  currTableName);
            strSql.Append(" values (");
            strSql.Append(model.grade.ToString()+ ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.grade_name) + "'");
            strSql.Append(")");

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0) 
            {
                if (!grades.ContainsKey(model.grade.ToString()))
                {
                    grades.Add(model.grade.ToString(), StringHelper.SqlFilter(model.grade_name));
                }
                return true;
            }
            else
            {
                return false;

            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param grade_name="model"></param>
        /// <returns></returns>
        public bool Update(UserGradeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " +  currTableName);
            strSql.Append(" set grade_name='" + StringHelper.SqlFilter(model.grade_name) + "'");
            strSql.Append(" where id=" + model.id);

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                if (grades.ContainsKey(model.grade.ToString()))
                {
                    grades[model.grade.ToString()] = StringHelper.SqlFilter(model.grade_name);
                }
                return true;
            }
            else
            {
                return false;

            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param grade_name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id= " + id);

            UserGradeInfo uinfo = GetUserGradeInfo(id);
            string key = (uinfo != null) ? uinfo.grade.ToString() : "";

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                if (grades.ContainsKey(key))
                {
                    grades.Remove(key);
                }
                return true;
            }
            else
            {
                return false;

            }
        }

        #endregion

        #region 获取列表


        public string GetGradeName(string dicGrade)
        {
            string reString = "";
            grades.TryGetValue(dicGrade, out reString);
            return reString;
        }


        /// <summary>
        /// 得到IDataReader
        /// </summary>
        /// <param grade_name="topCount">-1表示全部</param>
        /// <param grade_name="colNames"></param>
        /// <param grade_name="andWhere"></param>
        /// <returns></returns>
        public static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql="select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }



        public DataTable GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,grade,grade_name from " + currTableName);
            return DbHelper.GetDataTable(strSql.ToString());
        }


        public DataTable GetList(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,grade,grade_name from " + currTableName);
            strSql.Append(" where id=" + id);
            return DbHelper.GetDataTable(strSql.ToString());
        }

        #endregion
    }
}
